﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace V2TEK.NET.SH.Silverlight.Charts.Core.Schedule
{
    public abstract class TimeLayout<T>: Layout<T>, ITime, ILayoutProvider
    {
        public static DependencyProperty StartDateProperty = DependencyProperty.Register("StartDate", typeof(DateTime), typeof(TimeLayout<T>), new PropertyMetadata(DateTime.Now));
        public static DependencyProperty EndDateProperty = DependencyProperty.Register("EndDate", typeof(DateTime), typeof(TimeLayout<T>), new PropertyMetadata(DateTime.Now));

        #region ITimeLayout Members

        public DateTime StartDate
        {
            get { return (DateTime)GetValue(StartDateProperty); }
            set { SetValue(StartDateProperty, value); }
        }

        public DateTime EndDate
        {
            get { return (DateTime)GetValue(EndDateProperty); }
            set { SetValue(EndDateProperty, value); }
        }

        public double TotalMilliseconds
        {
            get
            {
                return EndDate.Subtract(StartDate).TotalMilliseconds;
            }
            set
            {
            }
        } 

        /// <summary>
        /// 每一像素对应的毫秒数目。Zoom越大，表示对象实际显示越宽
        /// </summary>
        public double Zoom
        {
            get
            {
                return GetZoom(base.ContentWidth);
            }
            set
            {
                if (this.ZoomChanged != null)
                    this.ZoomChanged(this, new PropertyChangeArgsWithValue<double> { PropertyName = "Zoom", NewValue = value, OldValue = this.Zoom });
                this.ContentWidth = this.TotalMilliseconds / value;
            }
        }

        private double GetZoom(double contentWidth)
        {
            if (contentWidth == 0)
                return 0;
            return this.TotalMilliseconds / contentWidth;
        }

        protected override void ContentWidthPropertyChanged(DependencyObject sender, DependencyPropertyChangedEventArgs args)
        {
            TimeLayout<T> obj = (sender as TimeLayout<T>);
            if(this.ZoomChanged != null)
                this.ZoomChanged(sender, new PropertyChangeArgsWithValue<double> { PropertyName = "ContentWidth", NewValue = GetZoom((double)args.NewValue), OldValue = GetZoom((double)args.OldValue) });
        }

        public event EventHandler<PropertyChangeArgsWithValue<double>> ZoomChanged;

        #endregion



        public bool HasStartDate
        {
            get;
            set;
        }

        public bool HasEndDate
        {
            get;
            set;
        }
    }
}
